//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Integrator.h"
using namespace Cephei::QL::Math::Integrals;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Math::Integrals::CIntegrator::CIntegrator (boost::shared_ptr<QuantLib::Integrator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegrator = NULL;
#endif
	_ppIntegrator = &childNative;
    
}
Cephei::QL::Math::Integrals::CIntegrator::CIntegrator (QuantLib::Integrator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phIntegrator = NULL;
#endif
	_ppIntegrator = new boost::shared_ptr<QuantLib::Integrator> (&childNative);
    
    _IntegratorOwner = owner;
    
}

Cephei::QL::Math::Integrals::CIntegrator::CIntegrator (CIntegrator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegrator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIntegrator = new boost::shared_ptr<QuantLib::Integrator> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Integrals::CIntegrator::CIntegrator (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegrator = NULL;
#endif
	if (!t->IsSubclassOf(CIntegrator::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Integrals::CIntegrator::CIntegrator (QuantLib::Handle<QuantLib::Integrator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIntegrator = &childNative;
	_ppIntegrator = &static_cast<boost::shared_ptr<QuantLib::Integrator>>(childNative.currentLink());
    
    _IntegratorOwner = owner;
}
Cephei::QL::Math::Integrals::CIntegrator::CIntegrator (QuantLib::Handle<QuantLib::Integrator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIntegrator = &childNative;
	_ppIntegrator = &static_cast<boost::shared_ptr<QuantLib::Integrator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Integrals::CIntegrator::CIntegrator (QuantLib::Integrator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntegrator = NULL;
#endif
	_ppIntegrator = new boost::shared_ptr<QuantLib::Integrator> (new QuantLib::Integrator (childNative));
    
}
#endif

Cephei::QL::Math::Integrals::CIntegrator::~CIntegrator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppIntegrator != NULL)
    {
	    delete _ppIntegrator;
        _ppIntegrator = NULL;
    }
}
Cephei::QL::Math::Integrals::CIntegrator::!CIntegrator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppIntegrator != NULL)
    {
	    delete _ppIntegrator;
    }
}
QuantLib::Integrator& Cephei::QL::Math::Integrals::CIntegrator::GetReference ()
{
    if (_ppIntegrator == NULL) throw gcnew NativeNullException ();
	return **_ppIntegrator;
}
boost::shared_ptr<QuantLib::Integrator>& Cephei::QL::Math::Integrals::CIntegrator::GetShared ()
{
    if (_ppIntegrator == NULL) throw gcnew NativeNullException ();
	return *_ppIntegrator;
}
QuantLib::Integrator* Cephei::QL::Math::Integrals::CIntegrator::GetPointer ()
{
    if (_ppIntegrator == NULL) throw gcnew NativeNullException ();
	return &**_ppIntegrator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Integrator>& Cephei::QL::Math::Integrals::CIntegrator::GetHandle ()
{
	if (_phIntegrator == NULL)
	{
		_phIntegrator = new Handle<QuantLib::Integrator> (*_ppIntegrator);
	}
	return *_phIntegrator;
}
#endif
bool Cephei::QL::Math::Integrals::CIntegrator::HasNative () 
{
	return (_ppIntegrator != NULL);
}

Double Cephei::QL::Math::Integrals::CIntegrator::AbsoluteAccuracy::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIntegrator)->absoluteAccuracy ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Integrals::CIntegrator::AbsoluteError::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIntegrator)->absoluteError ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::Integrals::CIntegrator::IntegrationSuccess::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppIntegrator)->integrationSuccess ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Integrals::CIntegrator::MaxEvaluations::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppIntegrator)->maxEvaluations ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Integrals::CIntegrator::NumberOfEvaluations::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppIntegrator)->numberOfEvaluations ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Integrals::IIntegrator^ Cephei::QL::Math::Integrals::CIntegrator::SetAbsoluteAccuracy (Double accuracy)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _accuracy = (QuantLib::Real)ValueHelper::Convert (accuracy);
    	(*_ppIntegrator)->setAbsoluteAccuracy ( _accuracy );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Integrals::IIntegrator^ Cephei::QL::Math::Integrals::CIntegrator::SetMaxEvaluations (UInt64 maxEvaluations)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _maxEvaluations = (QuantLib::Size)ValueHelper::Convert (maxEvaluations);
    	(*_ppIntegrator)->setMaxEvaluations ( _maxEvaluations );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

